package com.yfeng.goodjuhome.model;

import com.yfeng.common.dao.IDao;
import com.yfeng.common.dao.Param;
import com.yfeng.common.utils.Util;
import com.yfeng.exception.ModelException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class User {

    private final Log log = LogFactory.getLog(this.getClass());

    @Autowired
    public IDao baseDao;

    private String id; // 用户id
    private String username; // 用户名
    private String passwd; // 密码
    private String email; // email
    private String usertype; //usertype
    private String enable;
    private String homecode;  //家园号
    private String createtime;
    private String regdate;


    public User() {
    }

    public User(String id, String username, String passwd, String email, String usertype, String enable, String homecode, String createtime, String regdate) {
        this.id = id;
        this.username = username;
        this.passwd = passwd;
        this.email = email;
        this.usertype = usertype;
        this.enable = enable;
        this.homecode = homecode;
        this.createtime = createtime;
        this.regdate = regdate;
    }



    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPasswd() {
        return passwd;
    }

    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getUsertype() {
        return usertype;
    }

    public void setUsertype(String usertype) {
        this.usertype = usertype;
    }

    public String getEnable() {
        return enable;
    }

    public void setEnable(String enable) {
        this.enable = enable;
    }

    public String getHomecode() {
        return homecode;
    }

    public void setHomecode(String homecode) {
        this.homecode = homecode;
    }

    public String getCreatetime() {
        return createtime;
    }

    public void setCreatetime(String createtime) {
        this.createtime = createtime;
    }

    public String getRegdate() {
        return regdate;
    }

    public void setRegdate(String regdate) {
        this.regdate = regdate;
    }



    @Override
    public String toString() {
        return "User{" +
                "baseDao=" + baseDao +
                ", id='" + id + '\'' +
                ", username='" + username + '\'' +
                ", passwd='" + passwd + '\'' +
                ", email='" + email + '\'' +
                ", usertype='" + usertype + '\'' +
                ", enable='" + enable + '\'' +
                ", homecode='" + homecode + '\'' +
                ", createtime='" + createtime + '\'' +
                ", regdate='" + regdate + '\'' +
                '}';
    }

    public void setUser(User user)
    {
        this.id=user.getId();
        this.username=user.getUsername();
        this.passwd=user.getPasswd();
        this.email=user.getEmail();
        this.usertype=user.getUsertype();
        this.enable=user.getEnable();
        this.homecode=user.getHomecode();
        this.createtime=user.getCreatetime();
        this.regdate = user.getRegdate();
    }

    public User getUserById(String id) throws Exception {
        User user = null;
        List userList = baseDao.query("select t1.* from users t1 where  t1.id=:id", new Param("id", id));
        if (userList.size() > 0) {
            user = new User();
            Map userRow = (Map) userList.get(0);
            user.setId(id);
            user.setUsername(userRow.get("username").toString());
            user.setPasswd(userRow.get("passwd").toString());
            user.setEnable(userRow.get("enable").toString());
            user.setEmail(userRow.get("email").toString());
            user.setHomecode(userRow.get("homecode").toString());
            user.setUsertype(userRow.get("usertype").toString());
            return user;
        } else
            return null;
    }

    public User getUserByUsername(String username) throws Exception {
        User user = null;
        List userList = baseDao.query("select t1.* from users t1 where  t1.username=:username", new Param("username", username));
        if (userList.size() > 0) {
            user = new User();
            Map userRow = (Map) userList.get(0);
            user.setId(userRow.get("id").toString());
            user.setUsername(Util.getEmptyStr(userRow.get("username")));
            user.setPasswd(Util.getEmptyStr(userRow.get("passwd")));
            user.setEnable(Util.getEmptyStr(userRow.get("enable")));
            user.setEmail(Util.getEmptyStr(userRow.get("email")));
            user.setHomecode(Util.getEmptyStr(userRow.get("homecode")));
            user.setUsertype(Util.getEmptyStr(userRow.get("usertype")));
            return user;
        } else
            return null;
    }

    public boolean isExist(String username) throws Exception {

        int ret = baseDao.getRowCount("from users where username=:username", new Param("username", username));
        if (ret > 0)
            return true;
        else
            return false;

    }

    public boolean isExist(String username, String passwd) throws Exception {
        Param param = new Param();
        param.put("username", username);
        param.put("passwd", passwd);
        int ret = baseDao.getRowCount("from users where username=:username and passwd=:passwd", param);
        if (ret > 0)
            return true;
        else
            return false;

    }


    /**
     * 返回该用户的用户类型
     * User.getUserById("3").getUserType();
     *
     * @return
     * @throws Exception
     */
    public UserTypeModel getUserType() throws ModelException {
        try {
            List list = baseDao.query("select t2.* from users t1,mb_usertype t2  where t1.usertype=t2.id and t1.id='" + this.id + "' and t1.usertype='" + this.usertype + "'");
            UserTypeModel model = null;
            if (list.size() > 0) {
                Map map = (Map) list.get(0);
                model = new UserTypeModel();
                model.setId(map.get("id").toString());
                model.setName((String) map.get("name"));
                model.setCreatetime(map.get("createtime").toString());
                return model;
            }
            else
            {
                log.info("没有查到uid:"+this.id+",usertype:"+this.usertype+"的用户类型信息");
                return null;
            }
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new ModelException(e.getMessage(), e);
        }
        

    }


    /**
     * 判断是否为公司用户，如设计公司，装饰公司
     *
     * @return
     * @throws ModelException
     */
    public boolean isCompanyUser() throws ModelException {
        try {
            UserTypeModel model = getUserType();                
            if ("3".equals(model.getId()) || "4".equals(model.getId())) {
                return true;
            } else
                return false;

        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new ModelException(e.getMessage(), e);
        }

    }
}
